package com.bocloud.cmp.service.resource;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bocloud.cmp.dao.CloudVendorDao;
import com.bocloud.cmp.dao.DataStoreDao;
import com.bocloud.cmp.dao.VolumeDao;
import com.bocloud.cmp.entity.resource.CloudVendor;
import com.bocloud.cmp.entity.resource.DataStore;
import com.bocloud.cmp.entity.resource.Volume;
import com.bocloud.cmp.interfaces.resource.ResourceService;
import com.bocloud.cmp.service.model.VendorStore;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 平台资源集中接口实现类
 * 
 * @author weiwei
 *
 */
@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {

	private static Logger logger = LoggerFactory.getLogger(ResourceServiceImpl.class);
	@Autowired
	private DataStoreDao dataStoreDao;
	@Autowired
	private VolumeDao volumeDao;
	@Autowired
	private CloudVendorDao cloudVendorDao;

	@Override
	public BsmResult vendorStoreList() {
		try {
			List<VendorStore> stores = Lists.newArrayList();
			List<CloudVendor> vendors = cloudVendorDao.listAll();
			Map<Long, CloudVendor> vendorMap = Maps.newHashMap();
			for (CloudVendor vendor : vendors) {
				vendorMap.put(vendor.getId(), vendor);
			}
			List<DataStore> dataStores = this.dataStoreDao.list(1, Integer.MAX_VALUE, null, null);
			for (DataStore dataStore : dataStores) {
				stores.add(new VendorStore(dataStore.getId(), dataStore.getName(),
						vendorMap.get(dataStore.getVendorId()).getType(),
						vendorMap.get(dataStore.getVendorId()).getName(), dataStore.getType(),
						Integer.valueOf(dataStore.getCapacity().toString()), dataStore.getStatus()));
			}
			List<Volume> volumes = this.volumeDao.list(1, Integer.MAX_VALUE, null, null);
			for (Volume volume : volumes) {
				stores.add(new VendorStore(volume.getId(), volume.getName(),
						vendorMap.get(volume.getVendorId()).getType(), vendorMap.get(volume.getVendorId()).getName(),
						volume.getType(), volume.getSize(), volume.getStatus()));
			}
			return new BsmResult(true, stores, "查询成功");
		} catch (Exception e) {
			logger.error("List vendor store error:", e);
			return new BsmResult(false, "查询失败");
		}
	}

}
